package com.mr.web.controller;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import com.mr.model.BaseInfo;
import com.mr.model.Company;
import com.mr.model.Config;
import com.mr.model.Discount;
import com.mr.model.Member;
import com.mr.model.News;
import com.mr.model.Proxy;
import com.mr.model.QQInfo;
import com.mr.model.Shop;
import com.mr.model.Status;
import com.mr.model.TreeNode;
import com.mr.model.User;
import com.mr.model.Video;
import com.mr.util.StringUtil;

@Controller
public class IndexController extends AnnotationController{
	@RequestMapping("/index.xhtml")
	public String index(ModelMap model){
		return "index.vm";
	}
	@RequestMapping("/version.xhtml")
	public String version(ModelMap model) throws IOException{
		String version = System.currentTimeMillis()+"";
		Config.version = version;
		return forwadMessage(model, "当前版本：" + version);
	}
	@RequestMapping("/login.xhtml")
	public String login(String membername, String password, HttpServletRequest req, HttpServletResponse res, ModelMap model){
		//if(!isRightCK(req)) return alertMessage("验证码错误！", "index.xhtml", model);
		String qry = "from Member m where m.membername=? and m.password=?";
		List<Member> memberList = hibernateTemplate.find(qry, membername, StringUtil.md5(password));
		if(memberList.size()==0) return alertMessage("用户名或密码不正确！", "index.xhtml", model);		
		Member member = memberList.get(0);
		BaseInfo base = commonService.getBaseInfo(member.getId());
		log.warn("用户登录："+base.getName());
		if(StringUtils.equals(Status.D, base.getStatus())) return alertMessage("该用户已被管理员删除！", "index.xhtml", model);
		req.getSession().setAttribute(Config.AUTHNAME, base.getName());
		if(base==null) return alertMessage("非法用户！", "index.xhtml", model);
		
		if(base instanceof User) {
			req.getSession().setAttribute("isUser", true);
		}else if(base instanceof Shop){
			req.getSession().setAttribute("isShop", true);
		}else if(base instanceof Proxy) {
			req.getSession().setAttribute("isProxy", true);
		}else if(base instanceof Company){
			req.getSession().setAttribute("isCompany", true);
		}else {
			return alertMessage("非法用户！", "index.xhtml", model);
		}
		List<QQInfo> qqList = commonService.getQQInfoList(null);
		req.getSession().setAttribute("qqList", qqList);
		req.getSession().setAttribute("base", base);
		req.getSession().setAttribute(Config.MEMBERTYPE, member.getMembertype());
		return "redirect:/auth/public/desk.xhtml";
	}
	//用户详细
	@RequestMapping("/auth/public/desk.xhtml")
	public String desk(HttpServletRequest req, HttpServletResponse response, ModelMap model){
		BaseInfo base = (BaseInfo)req.getSession().getAttribute("base");
		Member submember = daoService.getObject(Member.class, base.getId());
		Member pm = daoService.getObject(Member.class, submember.getParentid());
		BaseInfo parentBase = commonService.getBaseInfo(pm.getId());
		model.put("pm", pm);
		model.put("parentBase", parentBase);
		
		List<TreeNode> myTreeList = new ArrayList<TreeNode>();
		String sysRole = "";
		if(base instanceof User) {
			myTreeList.addAll(getUserTreeList());
			sysRole = "普通用户";
		}else if(base instanceof Shop){
			myTreeList.addAll(getShopTreeList());
			sysRole = "零售店";
			
		}else if(base instanceof Proxy) {
			myTreeList.addAll(getProxyTreeList());
			sysRole = "代理公司";
		}else if(base instanceof Company){
			myTreeList.addAll(getCompanyTreeList());
			sysRole = "总系统";
			model.put("isCompany", true);
		}
		model.put("sysRole", sysRole);
		model.put("myTreeList", myTreeList);
		deskMap(req, model);
		return "desk.vm";
	}
	//用户菜单
	private List<TreeNode> getUserTreeList(){
		List<TreeNode> treeList = new ArrayList<TreeNode>();
		TreeNode t1 = new TreeNode("我的积分", "auth/member/user/pointList.xhtml", "icon-qq");
		treeList.add(t1);
		return treeList;
	}
	//零售店
	private List<TreeNode> getShopTreeList(){
		List<TreeNode> treeList = new ArrayList<TreeNode>();
		TreeNode t1 = new TreeNode("会员管理", "auth/member/shop/userList.xhtml", "icon-user");
		treeList.add(t1);
		return treeList;
	}
	//代理公司
	private List<TreeNode> getProxyTreeList(){
		List<TreeNode> treeList = new ArrayList<TreeNode>();
		TreeNode t1 = new TreeNode("市场计划", "auth/member/proxy/planList.xhtml", "icon-plan");
		TreeNode t2 = new TreeNode("零售店管理", "auth/member/proxy/shopList.xhtml", "icon-shop");
		TreeNode t3 = new TreeNode("商品管理", "auth/member/proxy/goodsList.xhtml", "icon-goods");
		treeList.add(t1);
		treeList.add(t2);
		treeList.add(t3);
		return treeList;
	}
	//总公司
	private List<TreeNode> getCompanyTreeList(){
		List<TreeNode> treeList = new ArrayList<TreeNode>();
		TreeNode t1 = new TreeNode("企业文化", "auth/member/company/cultureForm.xhtml", "icon-culture");
		TreeNode t2 = new TreeNode("企业风采", "auth/member/company/elegantForm.xhtml", "icon-elegant");
		TreeNode t3 = new TreeNode("公司新闻", "auth/member/company/newsList.xhtml", "icon-news");
		TreeNode t4 = new TreeNode("优惠信息", "auth/member/company/discountList.xhtml", "icon-discount");
		TreeNode t5 = new TreeNode("市场计划", "auth/member/company/planList.xhtml", "icon-plan");
		TreeNode t6 = new TreeNode("代理公司", "auth/member/company/proxyList.xhtml", "icon-proxy");
		TreeNode t7 = new TreeNode("零售店", "auth/member/company/shopList.xhtml", "icon-shop");
		TreeNode t8 = new TreeNode("零售店会员", "auth/member/company/userList.xhtml", "icon-user");
		TreeNode t9 = new TreeNode("产品管理", "auth/member/company/goodsList.xhtml", "icon-goods");
		TreeNode t10 = new TreeNode("远程教育", "auth/member/company/videoList.xhtml", "icon-video");
		TreeNode t11 = new TreeNode("在线咨询", "auth/member/company/qqList.xhtml", "icon-qq");
		TreeNode t12 = new TreeNode("授权管理", "auth/member/company/qxList.xhtml", "icon-qx");
		
		treeList.add(t1);
		treeList.add(t2);
		treeList.add(t3);
		treeList.add(t4);
		treeList.add(t5);
		treeList.add(t6);
		treeList.add(t7);
		treeList.add(t8);
		treeList.add(t9);
		treeList.add(t10);
		treeList.add(t11);
		treeList.add(t12);
		return treeList;
	}
	//后台主页
	public void deskMap(HttpServletRequest req, ModelMap model){
		BaseInfo base = (BaseInfo)req.getSession().getAttribute("base");
		List<Video> videoList = commonService.getVideoList(null, null, Status.Y, 0, 4);
		List<Discount> discountList = commonService.getDiscountList(base, null, null, null, Status.Y, 0, 10);
		List<News> newsList = commonService.getNewsList(base, null, Status.Y, 0, 10);
		model.put("newsList", newsList);
		model.put("videoList", videoList);
		model.put("discountList", discountList);
	}
	//退出
	@RequestMapping("/auth/public/exit.xhtml")
	public String exit(HttpServletRequest req, ModelMap model){
		req.getSession().removeAttribute("base");
		return "redirect:/index.xhtml";
	}
	//进行中
	@RequestMapping("/auth/public/process.xhtml")
	public String process(ModelMap model){
		return "process.vm";
	}
}
